define(function (require) {
    var app = require('app')
    .controller('analysisConfigCtrl', ['$scope','$rootScope', function($scope,$rootScope,$role){
		
		$scope.selectLayer=function(){
			if($rootScope.gislayerconfig.sRows==null || $rootScope.gislayerconfig.sRows.length<1){
				$.messager.alert("系统提示","请先选择一条记录");
				return;
			}
			
			if($rootScope.gislayerconfig.sRows.length>1){
				$.messager.alert("系统提示","请选择一条记录");
				return;
			}
			
			var tempRow=$.extend(true, {}, $rootScope.gislayerconfig.sRows[0]);
			if(tempRow!=null && tempRow.id!=null){
				tempRow.layerId=tempRow.id;
				delete tempRow.id;
			}
			
			var editRow=$rootScope.analysisconfig.editRow;
			editRow=$.extend(true, editRow, tempRow);
			$rootScope.analysisconfig.editRow=editRow;
			$rootScope.closeWindow('edit2');
		};
		
		/**
		 * 编辑当前记录
		 */
		$scope.editSelectRow=function(formName,row){
			$rootScope.editRow(formName,row);
			$rootScope.openWindow('edit');
			$rootScope.setTab('config', 'tab1');
			$scope.reloadTree(row);
	
		},
		
		/**
		 * 选择元数据变化时，处理方式
		 */
		$scope.onChangeMed=function(){
			setTimeout(function(){
				var temp=$rootScope["dicTree"].selectedRow;
				if(temp!=null){
					$rootScope["analysisconfig"].editRow["medalianame"]=temp.display;
					$rootScope["analysisconfig"].editRow.statisticsField=[];
					$rootScope["analysisconfig"].editRow.statisticsCateory=[];
					$rootScope["analysisconfig"].editRow.showField=[];
					$scope.reloadTree();
				}
			},100);
		};
		
		/**
		 * 重新加载统计类别和统计字段的树控件数据
		 */
		$scope.reloadTree=function(){

			if($rootScope["analysisconfig"].editRow.medname!=null &&
				$rootScope["analysisconfig"].editRow.medname!=''
			){
				$rootScope.c_metadata.queryObject["mid:="]=$rootScope["analysisconfig"].editRow.medname;
				$rootScope.f_metadata.queryObject["mid:="]=$rootScope["analysisconfig"].editRow.medname;
				$rootScope.s_metadata.queryObject["mid:="]=$rootScope["analysisconfig"].editRow.medname;
			}else{
				$rootScope.c_metadata.queryObject["mid:="]='novalue';
				$rootScope.f_metadata.queryObject["mid:="]='novalue';
				$rootScope.s_metadata.queryObject["mid:="]='novalue';
			}
			$rootScope.c_metadata.loadTreeData();
			$rootScope.f_metadata.loadTreeData();
			$rootScope.s_metadata.loadTreeData();
		};
		
		/**
		 * 元数据变化时，重新加载列表数据
		 * @param {Object} row
		 */
		$scope.onChangeMedName=function(row){
			/*if(row.medname!=null && row.medname!=""){
				$rootScope['c_metadata'].queryObject["mid:="]=row.medname;
				$rootScope['f_metadata'].queryObject["mid:="]=row.medname;
			}else{
				$rootScope['c_metadata'].queryObject["mid:="]=$rootScope.getGuid();
				$rootScope['f_metadata'].queryObject["mid:="]=$rootScope.getGuid();
			}*/
			//$rootScope.loadData('c_metadata');
			//$rootScope.loadData('f_metadata');
		}
		
		/**
		 * 编辑表单选中记录
		 * @param {Object} formName
		 */
		$scope.editFormSelectRow=function(formName){
			if($rootScope[formName].sRows==null || $rootScope[formName].sRows.length<1){
				$.messager.alert("系统提示","请先选择一条记录");
				return;
			}
			
			if($rootScope[formName].sRows.length>1){
				$.messager.alert("系统提示","请选择一条记录");
				return;
			}
			
			$scope.editSelectRow(formName,$rootScope[formName].sRows[0]);
		}
	}]);
});